﻿using app.Common;
using HLVControl.Grid;
using HLVControl.Grid.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Data.Objects.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eService.BLL
{
    //ROOM
    public class ExcuteDOITUONG
    {
        eServiceEntities dbEntities;
        IExecuteData<ROOM> Execute;
        public string ErrMsg { get; set; }
        public bool KQ { get; set; }

        public ExcuteDOITUONG()
        {
            dbEntities = ConnectionProvider.getDbEntities();
            Execute = new ExecuteData<ROOM>(dbEntities);
        }
        public List<ROOM> GetAll()
        {
            List<ROOM>  list = (List<ROOM>)Execute.GetAll();
            if (list != null && list.Count>0) KQ = true;
            return list;
        }
        public ROOM GetByID(int ID)
        {
            ROOM u = Execute.GetById(ID);
            if (u != null) KQ = true;
            return u;
        }
        public bool Add(ROOM Newobj)
        {
            KQ = false;
            if (Newobj != null)
            {
                KQ = Execute.Insert(Newobj);
                if (KQ)
                    ErrMsg = "Thêm thành công!";
                else
                    ErrMsg = "Không thể thêm !";
            }
            else
            {
                ErrMsg = "Vui lòng nhập thông tin đầy đủ!";
            }
            return KQ;
        }
        public bool UpDate(ROOM obj)
        {
            KQ = false;
            if (obj != null)
            {
                ROOM obj1 = dbEntities.ROOMs.Find(obj.RID);
                if (obj1 != null)
                {
                    KQ = Execute.Update(obj, obj1);
                    if (KQ)
                        ErrMsg = "Cập nhật thành công!";
                    else
                        ErrMsg = "Không thể cập nhật!";
                }
            }
            else
            {
                ErrMsg = "Vui lòng chọn thông tin!";
            }
            return KQ;
        }
        public bool Delete(ROOM obj)
        {
            KQ = false;
            if (obj != null)
            {
                ROOM obj1 = dbEntities.ROOMs.Find(obj.RID);
                if (obj != null)
                {
                    KQ = Execute.Delete(obj1);
                    if (KQ)
                        ErrMsg = "xoá thành công!";
                    else
                        ErrMsg = "Không thể xoá!";
                }
            }
            else
            {
                ErrMsg = "Vui lòng chọn!";
            }
            return KQ;
        }
        public ROOM LoadValues(int Ma,int MaKhuVuc, string Ten, double DonGia, double DonGia2, double DonGia3, double DonGia4, double DonGia5, int STT, string GhiChu)
        {
            KQ = false;
            ROOM obj = new ROOM();
            if (Ten != "" && MaKhuVuc>0)
            {
                obj.RID = Ma;
                obj.RName = Ten;
                obj.RPrice = DonGia;
                obj.Price2 = DonGia2;
                obj.Price3 = DonGia3;
                obj.Price4 = DonGia4;
                obj.Price5 = DonGia5;
                obj.RDes = GhiChu;
                obj.GrID = MaKhuVuc;
                obj.Poss = STT;
                KQ = true;
            }
            else
            {
                obj = null;
                ErrMsg = "Vui lòng điền tên!";
            }
            return obj;
        }

        public DataTable ToDataTable(List<ROOM> list)
        {
            DataTable dtData = Execute.ToDataTable<ROOM>(list);
            return dtData;
        }
        public bool LoadToTreeview(TreeListView treeview, List<ROOM> list)
        {
            KQ = false;
            ExcuteKHUVUC exeKV = new ExcuteKHUVUC();
            treeview.Rows.Clear();
            int count = 1;
            foreach (ROOM obj in list)
            {
                TreeListRow row = treeview.CreateRow();
                row.Cells.Add(new TreeListCell(count));
                row.Cells.Add(new TreeListCell(obj.RID));
                row.Cells.Add(new TreeListCell(obj.RName));

                GROUPROOM objG = exeKV.GetByID((int)obj.GrID);
                if (objG == null) row.Cells.Add(new TreeListCell(""));
                else row.Cells.Add(new TreeListCell(objG.GrName));

                row.Cells.Add(new TreeListCell(obj.RPrice));
                row.Cells.Add(new TreeListCell(obj.Price2));
                row.Cells.Add(new TreeListCell(obj.Price3));
                row.Cells.Add(new TreeListCell(obj.Price4));
                row.Cells.Add(new TreeListCell(obj.Price5));
                row.Cells.Add(new TreeListCell(obj.Poss));
                row.Cells.Add(new TreeListCell(obj.RDes));

                treeview.Rows.Add(row);
                count++;
                KQ = true;
            }
            treeview.Refresh();
            if (!KQ)
                ErrMsg = "Không thể tải dữ liệu!";
            return KQ;
        }
    }
}
